package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Log;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.MapperRow;

public class LogDao {

	private DBHelp<Log> db = new DBHelp<Log>();
	
	public void save(Log log) {
		String sql = "INSERT INTO t_log(aid,nowtime,getmoney,savemoney,note,balance) VALUES(?,?,?,?,?,?)";
		db.excuteSQL(sql, log.getAid(),log.getNowtime(),log.getGetmoney(),log.getSavemoney(),log.getNote(),log.getBalance());
	}

	public List<Log> findAllByAid(int id) {
		String sql = "SELECT id,aid,nowtime,getmoney,savemoney,note,balance FROM t_log WHERE aid=? order by nowtime desc";
		return db.queryForList(sql, new LogMapper(), id);
	}
	
	private class LogMapper implements MapperRow<Log> {

		public Log rowMapper(ResultSet rs) throws SQLException {
			Log log = new Log();
			log.setAid(rs.getInt("aid"));
			log.setBalance(rs.getFloat("balance"));
			log.setGetmoney(rs.getFloat("getmoney"));
			log.setId(rs.getInt("id"));
			log.setNote(rs.getString("note"));
			log.setNowtime(rs.getString("nowtime"));
			log.setSavemoney(rs.getFloat("savemoney"));
			return log;
		}
		
	}
}
